<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of az_Category
 *
 * @author Shark
 */
class Az_Category {

    private $nametbl = 'az_category';
    var $category_id;
    var $name;
    var $description;
    var $status;
    var $date_create;
    var $date_edit;
    var $user_id;
    var $slug;
    var $cat_parent;
    var $type;
    var $survey_id;

    /*
     * Function add value for category
     */

    public function create_category($catId, $name, $des, $status, $date_create, $date_edit, $user_id, $slug, $cat_parent, $type, $survey_id) {
        $this->category_id = $catId;
        $this->name = $name;
        $this->description = $des;
        $this->status = $status;
        $this->date_create = $date_create;
        $this->date_edit = $date_edit;
        $this->user_id = $user_id;
        $this->slug = $slug;
        $this->cat_parent = $cat_parent;
        $this->type = $type;
        $this->survey_id = $survey_id;
    }

    /*
     * Function add new category
     */

    function add_new_category() {
        global $az_db;
        if ($this->name != null) {
            $sql = "INSERT INTO " . $this->nametbl .
                    "(`name`,`description`,`status`,`date_create`,`date_edit`," .
                    "`user_id`,`slug`,`category_parent_id`,`type`,`survey_id`)" .
                    "VALUES(?,?,?,?,?,?,?,?,?,?)";
            $params = array($this->name, $this->description, $this->status, $this->date_create,
                $this->date_edit, $this->user_id, $this->slug, $this->cat_parent, $this->type, $this->survey_id);
            return $az_db->executeStmt_Az($sql, 'ssissisiii', $params);
        }
    }

    /*
     * Function update a category
     */

    function update_category() {
        global $az_db;
        if ($this->name != null) {
            $type_format = '';
            $params = array();
            $sql = "UPDATE " . $this->nametbl . " SET name = ?";
            $type_format.='s';
            array_push($params, $this->name);
            if ($this->description != null) {
                $sql.= ",description = ?";
                $type_format.='s';
                array_push($params, $this->description);
            }
            if ($this->status != null && is_numeric($this->status)) {
                $sql.= ",status = ?";
                $type_format .='i';
                array_push($params, $this->status);
            }
            if ($this->date_edit != null) {
                $sql.= ",date_edit = ?";
                $type_format .='s';
                array_push($params, $this->date_edit);
            }
            if ($this->user_id != null) {
                $sql.=",user_id =?";
                $type_format .='i';
                array_push($params, $this->user_id);
            }
            if ($this->slug != null) {
                $sql.=",slug =?";
                $type_format .='s';
                array_push($params, $this->slug);
            }
            if ($this->cat_parent != null) {
                $sql.=",category_parent_id =?";
                $type_format .='i';
                array_push($params, $this->cat_parent);
            }
            if ($this->type != null) {
                $sql.=",type =?";
                $type_format .='i';
                array_push($params, $this->type);
            }
            if ($this->survey_id != null) {
                $sql.=",survey_id=?";
                $type_format .='i';
                array_push($params, $this->survey_id);
            }
            $sql.= " WHERE category_id = ?";
            $type_format.='i';
            array_push($params, $this->category_id);
            return $az_db->executeStmt_Az($sql, $type_format, $params);
        }
    }

    /*
     * Function add delete category
     */

    function delete_category($catId) {
        if ($catId != null && is_numeric($catId)) {
            global $az_db;
            $sql = "DELETE FROM " . $this->nametbl . " WHERE category_id =" . $catId;
            if ($this->update_stt_parent_category($catId)) {
                if ($az_db->executeSQL_Query($sql)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }

    /*
     *  Thực hiện update category parent
     *  Khi delete một category. Nếu nó là root và có con 
     *  Thì chuyển các con của nó lên là root
     *  Còn nó không phải là root mà có con
     *  Thì chuyển các con của nó lên làm con của cha nó
     */

    function update_stt_parent_category($catId) {
        global $az_db;
        $sql_get_parent = "SELECT `category_parent_id` FROM " . $this->nametbl . " WHERE `category_id`=" . $catId;
        $result_P = $az_db->executeSQL_Query($sql_get_parent);
        if ($result_P->num_rows > 0) {
            $rows_parent = $result_P->fetch_row();
            if ($rows_parent[0] == 0) {
                //Get all category child
                $sql_get_child = "SELECT * FROM " . $this->nametbl . " WHERE `category_parent_id`=" . $catId;
                $result_PP = $az_db->executeSQL_Query($sql_get_child);
                if ($result_PP->num_rows > 0) {
                    $total_child = $result_PP->num_rows;
                    while ($rows = $result_PP->fetch_array(MYSQLI_ASSOC)) {
                        if ($this->update_parent(0, $rows['category_id']))
                            $total_child--;
                    }
                    if ($total_child == 0) {
                        return true;
                    } else {
                        return false;
                    }
                }else{
                    return true;
                }
            } else {
                $sql_get_child = "SELECT * FROM " . $this->nametbl . " WHERE `category_parent_id`=" . $catId;
                $result_P = $az_db->executeSQL_Query($sql_get_child);
                if ($result_P->num_rows > 0) {
                    $total_child = $result_P->num_rows;
                    //Get parent
                    $sql = "SELECT `category_id` FROM " . $this->nametbl . " WHERE `category_id`=" . $rows_parent[0];
                    $parent = $az_db->executeSQL_Query($sql)->fetch_row();
                    while ($rows_child = $result_P->fetch_array(MYSQLI_ASSOC)) {
                        if ($this->update_parent($parent[0], $rows_child['category_id']))
                            $total_child--;
                    }
                    if ($total_child == 0) {
                        return true;
                    } else {
                        return false;
                    }
                }
                 return true;
            }
        } else {
            return true;
        }
    }

    //Function update parent value
    function update_parent($parent_new, $catId) {
        global $az_db;
        $sql = "UPDATE " . $this->nametbl . " SET category_parent_id=" . $parent_new . " WHERE category_id=" . $catId;
        return $az_db->executeSQL_Query($sql);
    }

    /*
     * Function check avaliable
     */

    function check_avaliable() {
        
    }

}

?>
